﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using AncientEmpires_025_132.ResourceString;

namespace AncientEmpires_025_132
{
    public class MyButton : VisibleGameEntity
    {
        

        private bool _isClick;
        public bool IsClick
        {
            get { return _isClick; }
            set { _isClick = value; }
        }

        private bool _isPressed;
        public bool IsPressed
        {
            get { return _isPressed; }
            set { _isPressed = value; }
        }

        

        private MouseState mouseState;
        private MouseState oldMouseState;
        // Sự kiện khi button được click.
        public delegate void EventHandleClick();
        public event EventHandleClick ClickButton;

        public delegate void EventHandleClickWithSender(object sender);
        public event EventHandleClickWithSender ClickButtonWithSender;

        public MyButton(ContentManager content, List<string> strtexture, Vector2 pos, Vector2 size, Vector2 topleft)
        {
            //SoundMyEffect = content.Load<SoundEffect>(StringGame.SoundEffectButton);
            List<string> streffect = new List<string>();
            streffect.Add(StringGame.SoundEffectButton);
            MySound = new Sound(content, null, streffect);
            IsHide = false;
            IsHover = false;
            IsClick = false;
            ListSprites = new List<Sprite2D>();
            SizeScale = new Vector2(1f, 1f);
            for (int i = 0; i < strtexture.Count; i++)
            {
                Texture2D[] texture = new Texture2D[1];
                texture[0] = content.Load<Texture2D>(strtexture[i]);
                Sprite2D tempSprite = new Sprite2D(texture, pos);
                tempSprite.TopLeft = topleft;
                tempSprite.Size = size;
                ListSprites.Add(tempSprite);
                SizeTexture = new Vector2(texture[0].Bounds.Width, texture[0].Bounds.Height);
            }
            
            ISprite = 0;
            
        }

        public MyButton(ContentManager content, Sprite2D normal, Sprite2D hover, Sprite2D click)
        {
            IsHide = false;
            IsHover = false;
            IsClick = false;
            //SoundMyEffect = content.Load<SoundEffect>(StringGame.SoundEffectButton);
            List<string> streffect = new List<string>();
            streffect.Add(StringGame.SoundEffectButton);
            MySound = new Sound(content, null, streffect);
            ListSprites = new List<Sprite2D>();
            normal.TopLeft = -normal.Position;
            hover.TopLeft = -hover.Position;
            click.TopLeft = -click.Position;
            ListSprites.Add(normal);
            ListSprites.Add(hover);
            ListSprites.Add(click);
        }

        public override void Update(GameTime gameTime)
        {
            UpdateMouseState();
            if (IsPressed == true)
            {
                ISprite = 2;
            }
            else if (IsHover == true)
            {
                ISprite = 1;
            }
            else
            {
                ISprite = 0;
            }
        }

        public override void Draw(GameTime gameTime, SpriteBatch spriteBatch)
        {
            if (!IsHide)
            {
                base.Draw(gameTime, spriteBatch);
            }
        }
        public override void UpdateMouseState()
        {
            mouseState = Mouse.GetState();
            if (mouseState.X > ListSprites[0].Position.X && mouseState.X < ListSprites[0].Position.X + ListSprites[0].Size.X
                && mouseState.Y > ListSprites[0].Position.Y && mouseState.Y < ListSprites[0].Position.Y + ListSprites[0].Size.Y)
            {
                if (mouseState.LeftButton == ButtonState.Released && oldMouseState.LeftButton == ButtonState.Pressed)
                {
                    //SoundMyEffect.Play();
                    MySound.PlayEffect(Volume.MyVolume);
                    if (!IsHide) IsClick = true;
                    if (ClickButton!=null) ClickButton();
                    else if (ClickButtonWithSender != null) ClickButtonWithSender(this);
                    IsHover = false;
                    IsPressed = false;
                }
                else if (mouseState.LeftButton == ButtonState.Pressed)
                {
                    IsClick = false;
                    IsPressed = true;
                    IsHover = false;
                }
                else
                {
                    IsClick = false;
                    IsHover = true;
                    IsPressed = false;
                }
            }
            else
            {
                IsHover = false;
                IsClick = false;
                IsPressed = false;
            }
            oldMouseState = mouseState;
        }

        public Vector2 GetPosition()
        {
            return ListSprites[0].Position;        
        }

        public Vector2 GetSize()
        {
            return ListSprites[0].Size;
        }

        public void SetPosition(Vector2 setPos)
        {
            for (int i = 0; i < ListSprites.Count; i++)
            {
                ListSprites[i].Position = setPos;
            }
        }
        public void SetSize(Vector2 size)
        {
            for (int i = 0; i < ListSprites.Count; i++)
            {
                ListSprites[i].Size = size;
            }
        }
    }
}
